<template>

  <el-main>

    <el-form :inline="true" :model="user" ref="user" :rules="rules">

      <el-form-item label="登录名称" prop="username">
        <el-input v-model="user.username" placeholder="请输入登录名称"></el-input>
      </el-form-item>

      <el-form-item label="登录密码" prop="password">
        <el-input v-model="user.password" type="password" placeholder="请输入登录密码"></el-input>
      </el-form-item>

      <el-form-item label="验证码" prop="imgcode">
        <el-input  v-model="user.imgcode" placeholder="请输入验证码"></el-input>
       <el-image :src="imagecodeurl" @click="changeCode"></el-image>
      </el-form-item>
      <el-row>
        <el-button @click="login" type="success">登录</el-button>
        <el-button @click="reset" type="success">重置</el-button>

        <el-button @click="forgotPWD" type="success">忘记密码</el-button>

        <router-link to="/phoneLogin" >手机登录</router-link>
      </el-row>

    </el-form>
    <div>

      <el-dialog title="忘记密码" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="邮箱地址" :label-width="formLabelWidth">
            <el-input v-model="form.mail" autocomplete="off"></el-input>
            <el-button @click="sendCode" type="success">获取邮箱验证码</el-button>
          </el-form-item>
          <el-form-item label="输入验证码" :label-width="formLabelWidth">
            <el-input v-model="form.code" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </el-main>
</template>


<script>
import Vue from "vue";
import uuid from "uuid";

export default {
  data(){
    return {
      imagecodeurl:"",
      user:{
        username:'admin',
        password:'admin',
        imgcode:'',//验证
        uuid:'',//唯一标识
      },
      form:{
        mail:'',
        code:''
      },
      dialogFormVisible:false,
      formLabelWidth:'120px',
      rules:{
        username:[
          { required: true, message: '请输入登录名', trigger: 'blur' }
        ],
        password:[
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    sendCode(){
      this.axios.get(this.$baseUrl+'user/sendMail',{params:{mail:this.form.mail}}).then(res=>{
       this.$message(rs.data.msg)
      })
    },
    forgotPWD(){
      this.dialogFormVisible=true;
    },
    reset(){
      this.user.username="";
      this.user.password="";
      this.user.imgcode="";
    },
    changeCode(){
      this.imagecodeurl=  this.$baseUrl+'auth/getImgCode?t='+new Date()+"&uuid="+this.user.uuid
    },
    login(){
      let user = this.user;
      //发送ajax请求，后台验证用户，如果验证成功，给前台返回一个token（用户id或登录名称）
      this.axios.post(this.$baseUrl+'auth/login',user).then(res=>{
        if(res.data.data){

          //存储cookie 有效期是1天
         this.$cookies.set("tokenId",res.data.data.token,60*60*24)
          this.$router.push("/")//跳转页面
        }else{
          this.$message(res.data.msg);
        }
      })
    }
  },created() {
    const uuid = require('uuid')

    this.user.uuid=uuid.v1();

    this.imagecodeurl=this.$baseUrl+'auth/getImgCode?uuid='+this.user.uuid
  }
}
</script>

<style scoped>
.el-form {
  width: 40%;
  margin-left: 30%;
  margin-top: 10%;
}
</style>
